﻿using System;
using System.Data;
using Blog.Common.Helpers;
using Business;
using Common.Constant;

public partial class _Default : System.Web.UI.Page
{
    private PostBus _postBus;
    private DataSet _dataSet;
    private string _pageTitle;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if(Request.QueryString["user"] != null && Request.QueryString["user"] != "")
            {
                LoadByUserName(Request.QueryString["user"]);
            }
            else if(Request.QueryString["date"] != null && Request.QueryString["date"] != "")
            {
                LoadByPostDate(Request.QueryString["date"]);
            }
            else if(Request.QueryString["cid"] != null && Request.QueryString["cid"] != "")
            {
                try
                {
                    LoadByCategoryId(int.Parse(Request.QueryString["cid"]));
                }
                catch (Exception)
                {
                    lblMessage.Text = UIMessage.NoPostInThisCat;
                }
                
            }
            else if(Request.QueryString["view"] == "topview")
            {
                LoadTopViewPost(DBSchemas.NumberOfTopViewPost);
            }
            else if(Request.QueryString["view"] == "topcomment")
            {
                LoadTopCommentPost(DBSchemas.NumberOfTopCommentPost);
            }
            else
            {
                LoadDefault(DBSchemas.NumberOfTopPost);
            }
        }
        Page.Title = _pageTitle;
    }
    public void LoadDefault(int numberOfPost)
    {
        _postBus = new PostBus();
        _dataSet = _postBus.GetNewPost(numberOfPost);
        if (_dataSet.Tables[0].Rows.Count > 0)
        {
            rptListNewPost.DataSource = _dataSet;
            rptListNewPost.DataBind();
            lblMessage.Text = UIMessage.ViewNewPost;
            _pageTitle = UIMessage.ViewNewPost;
        }
        else
        {
            lblMessage.Text = UIMessage.NoPost;
            _pageTitle = UIMessage.BlogName;
        }
    }

    public void LoadByCategoryId(int catId)
    {
        _postBus = new PostBus();
        _dataSet = _postBus.GetPostByCategoryId(catId);
        if (_dataSet.Tables[0].Rows.Count > 0)
        {
            rptListNewPost.DataSource = _dataSet;
            rptListNewPost.DataBind();
            lblMessage.Text = UIMessage.ViewPostAt + _dataSet.Tables[0].Rows[0]["CategoryName"];
            _pageTitle = lblMessage.Text;
        }
        else
        {
            lblMessage.Text = UIMessage.NoPostInThisCat;
            _pageTitle = UIMessage.BlogName;
        }
    }

    public void LoadByUserName(string userName)
    {
        try
        {
            string userNameDecode = CryptographyHelper.Base64Decode(userName);
            _postBus = new PostBus();
            _dataSet = _postBus.GetPostByUser(userNameDecode);
            if (_dataSet.Tables[0].Rows.Count > 0)
            {
                rptListNewPost.DataSource = _dataSet;
                rptListNewPost.DataBind();
                lblMessage.Text = UIMessage.ViewPostBy + userNameDecode;
                _pageTitle = UIMessage.ViewPostBy + userNameDecode;
            }
            else
            {
                lblMessage.Text = UIMessage.UserHasNoPost;
                _pageTitle = UIMessage.BlogName;
            }
        }
        catch (Exception)
        {
            lblMessage.Text = UIMessage.UserHasNoPost;
            _pageTitle = UIMessage.BlogName;
        }
    }
    public void LoadByPostDate(string postDate)
    {
        try
        {
            string postDateDecode = CryptographyHelper.Base64Decode(postDate);
            string[] dateSplit = postDateDecode.Split('/');
            string date = dateSplit[1] + "/" + dateSplit[0] + "/" + dateSplit[2];
            _postBus = new PostBus();
            _dataSet = _postBus.GetPostByDate(date);
            if (_dataSet.Tables[0].Rows.Count > 0)
            {
                rptListNewPost.DataSource = _dataSet;
                rptListNewPost.DataBind();
                lblMessage.Text = UIMessage.ViewPostIn + postDateDecode;
                _pageTitle = UIMessage.ViewPostIn + postDateDecode; ;
            }
            else
            {
                lblMessage.Text = UIMessage.DateHasNoPost;
                _pageTitle = UIMessage.BlogName;
            }
        }
        catch (Exception)
        {
            lblMessage.Text = UIMessage.UserHasNoPost;
            _pageTitle = UIMessage.BlogName;
        }
    }
    public void LoadTopViewPost(int numberOfPost)
    {
        _postBus = new PostBus();
        _dataSet = _postBus.GetTopViewPost(numberOfPost);
        if (_dataSet.Tables[0].Rows.Count > 0)
        {
            rptListNewPost.DataSource = _dataSet;
            rptListNewPost.DataBind();
            lblMessage.Text = UIMessage.ViewTopViewPost;
            _pageTitle = UIMessage.ViewTopViewPost;
        }
        else
        {
            lblMessage.Text = UIMessage.NoPost;
            _pageTitle = UIMessage.BlogName;
        }
    }

    public void LoadTopCommentPost(int numberOfPost)
    {
        _postBus = new PostBus();
        _dataSet = _postBus.GetTopCommentPost(numberOfPost);
        if (_dataSet.Tables[0].Rows.Count > 0)
        {
            rptListNewPost.DataSource = _dataSet;
            rptListNewPost.DataBind();
            lblMessage.Text = UIMessage.ViewTopCommentPost;
            _pageTitle = UIMessage.ViewTopCommentPost;
        }
        else
        {
            lblMessage.Text = UIMessage.NoPost;
            _pageTitle = UIMessage.BlogName;
        }
    }


}
